//
//  YKDrugPriceDetailViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/4.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKDrugPriceDetailViewController.h"
#import "YKDrugPriceDetailHeaderView.h"
#import "YKClientListHeaderView.h"
#import "YKApproveDetailItem.h"
#import "YKPopupView.h"
#import "YKProcessTrackingViewController.h"
#import "YKTurnOverViewController.h"
#import "YKClientDrugPriceDetailModel.h"
#import "YKClientDrugPriceDetailDetailItem.h"
#import "YKAddYKDrugPriceViewController.h"
#import "YKApproveBottomView.h"
#import "WorkFlowManager.h"

@interface YKDrugPriceDetailViewController ()
@property (nonatomic, strong) YKClientDrugPriceDetailModel *mYKClientDrugPriceDetailModel;
@property (nonatomic, strong) YKDrugPriceDetailHeaderView *headerView;
@end

@implementation YKDrugPriceDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"协议价格详情";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    if (self.mYKClientDrugPriceModel) {
        self.ID = self.mYKClientDrugPriceModel.ID;
    }
    YKDrugPriceDetailHeaderView *headerView = [[NSBundle mainBundle]loadNibNamed:@"YKDrugPriceDetailHeaderView" owner:self options:nil].firstObject;
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    self.headerView = headerView;
    YKClientListHeaderView *listHeaderView = [[YKClientListHeaderView alloc]initWithTitles:@[@"药品信息",@"类型",@"协议价"] weights:@[@3,@1,@1] spaceing:0 leading:3 trailing:3];
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    [self.view addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    UIButton* processTrackButton = [[UIButton alloc]init];
    [self.view addSubview:processTrackButton];
    UIButton *editButton = [[UIButton alloc]init];
    [self.view addSubview:editButton];
    UIButton *submitButton = [[UIButton alloc]init];
    [self.view addSubview:submitButton];
    [processTrackButton setTitle:@"流程跟踪" forState:UIControlStateNormal];
    [editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [submitButton setTitle:@"提交审批" forState:UIControlStateNormal];
    [processTrackButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];
    [editButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];
    [submitButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];
    
    [processTrackButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];
    [editButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];
    [submitButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];
    
    [processTrackButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [editButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [submitButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    processTrackButton.titleLabel.font = [UIFont systemFontOfSize:14];
    editButton.titleLabel.font = [UIFont systemFontOfSize:14];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [submitButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    [processTrackButton addTarget:self action:@selector(processTrack:) forControlEvents:UIControlEventTouchUpInside];
    [editButton addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    
    [@[editButton,processTrackButton,submitButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[editButton,processTrackButton,submitButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(listHeaderView.mas_bottom);
        make.bottom.equalTo(processTrackButton.mas_top);
    }];
    
    if ([self.mYKClientDrugPriceModel.EXAMINESTATUS isEqualToString:@"2"] || [self.mYKClientDrugPriceModel.EXAMINESTATUS isEqualToString:@"4"]) {
        editButton.enabled = NO;
        submitButton.enabled = NO;
    }
    
    
    
    if (self.approveStatus) {
        [YKApproveBottomView addSelfToView:self ID:self.ID approveStatus:self.approveStatus];
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"customerDrugPriceManager/searchCustomerDrugPriceManagerById.htm" parameters:@{@"id":self.ID} success:^(id responseObject) {
        self.mYKClientDrugPriceDetailModel = [YKClientDrugPriceDetailModel yy_modelWithJSON:responseObject];
        self.mYKClientDrugPriceDetailModel.APPLYDATE = self.mYKClientDrugPriceModel.APPLYDATE;
        self.mYKClientDrugPriceDetailModel.APPLYORDERODE = self.mYKClientDrugPriceModel.APPLYORDERODE;
        self.mYKClientDrugPriceDetailModel.C_NAME = self.mYKClientDrugPriceModel.C_NAME;
        [self updateViews];
        [self tf_showStateView:kTFViewStateNone];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
}

- (void)updateViews {
    [self.tableViewManager removeAllSections];
    self.headerView.mYKClientDrugPriceDetailModel = self.mYKClientDrugPriceDetailModel;
    TFTableViewSection *section = [TFTableViewSection section];
    section.headerHeight = .1f;
    section.footerHeight = .1f;
    for (YKClientDrugPriceDetailDetailModel *model in self.mYKClientDrugPriceDetailModel.customerDrugPriceDetail) {
        YKClientDrugPriceDetailDetailItem *item = [YKClientDrugPriceDetailDetailItem itemWithModel:model];
        //item.cellHeight = 95;
        [section addItem:item];
    }
    [self.tableViewManager addSection:section];
    [self.tableView reloadData];
}

- (void)edit:(id)sender {
    YKAddYKDrugPriceViewController *vc = [[YKAddYKDrugPriceViewController alloc]init];
    vc.mYKClientDrugPriceDetailModel = self.mYKClientDrugPriceDetailModel;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)processTrack:(id)sender {
    YKProcessTrackingViewController *vc = [YKProcessTrackingViewController new];
//    vc.m_id = @"ORDER201710171940180003";
    vc.m_id = self.ID;
    [YKPopupView showPopupViewWithViewController:vc];
}
- (void)submit:(id)sender {
    [WorkFlowManager submitToApproveWithBusinessID:self.mYKClientDrugPriceDetailModel.customerDrugPrice.ID
                                fromViewController:self];
}


@end
